package com.fanshuai.resource;

import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Component;

import java.io.File;

/**
 * spring 文件处理
 * Resource为IO抽象  ClassPathResource  FileSystemResource
 * ResourceLoader获取Resource  Spring容器中有全局的ResourceLoader，为ApplicationContext本身
 */
@Slf4j
@Component
public class ResourceDemo {
    @javax.annotation.Resource
    private ResourceLoader resourceLoader;

    public void test() throws Exception {
        Resource resource = new ClassPathResource("r.txt");

        File file = resource.getFile();
        log.info(file.getAbsolutePath());

        Resource resource2 = new FileSystemResource("/Users/fanshuai/直播");
        file = resource2.getFile();

        String[] list = file.list();
        if (null != list) {
            for (String s : list) {
                log.info(s);
            }
        }
    }

    public void testResourceLoader() throws Exception {
        Resource resource = resourceLoader.getResource("r.txt");
        File file = resource.getFile();
        log.info(file.getAbsolutePath());
    }
}
